﻿<%@ Page Language="C#" AutoEventWireup="True" CodeBehind="staff.aspx.cs" Inherits="OpenRAMS.Staff.WebForm1" %>
<%@ Import Namespace="OpenRAMS.Model" %>

<!DOCTYPE html>

 <%
    //uses User ID to grab friendly first name. Is used later in page code
    String userName = User.Identity.Name.ToString();
    UserRecord test = new UserRecord();
    test.Load(userName);
    String coloquialName = test.userFirstName;
       
 %>
<html>
<head id="Head1" runat="server">
    <title>OpenRAMS - Staff Portal</title>
    <link rel="Stylesheet" type="text/css" href="../Include/default.css" />
    <link rel="Stylesheet" type="text/css" href="../Include/staff.css" />
</head>
<body>
    <table id="staffLayout" class="tableDefault">
    <tr>
    <td colspan="2" align="center"><div class="tableTitle">OpenRAMS Staff Portal</div>
    <div class="navBar">
    <form id="links" runat="server"> 
    <a href="staff.aspx" class="navLink">Home</a>
    <%
        //Code to show/hide links.
        if (!User.IsInRole("admin"))
        {
            adlink.Visible = false;
        }
        if (!User.IsInRole("maintenance") && !User.IsInRole("admin"))
        {
            mlink.Visible = false;
        }
        if (!User.IsInRole("community") && !User.IsInRole("admin"))
        {
            clink.Visible = false;
        }
      %>

    <asp:HyperLink ID="clink" NavigateUrl="~/Community/Community.aspx" runat="server" Text="Community " CssClass="navLink" />
    <asp:HyperLink ID="mlink" NavigateUrl="~/Maintenance/Maintenance.aspx" runat="server" Text="Maintenance " CssClass="navLink" /> 
    <asp:HyperLink ID="adlink" NavigateUrl="~/Admin/Admin.aspx" runat="server" Text="Admin " CssClass="navLink" />
    <a href="logout.aspx" class="navLink">Logout</a>
    </form>
    </div></td>
    </tr>
    <tr>
    <td colspan="2">
    <marquee behavior="scroll" direction="left" class="newsFlash">
    <% 
        Response.WriteFile(Server.MapPath("~/Include/newsflash/staff.txt"));
     %>    
     </marquee>
    </td>
    </tr>
    <tr>
    <td width="50%" valign="top">
    
        <table class="tableDefault" width="100%">
        <tr>
        <td align="center"><div class="tableTitle">
        <%=coloquialName %>'s Bookings</div>
        <div class="navBar"><a href="RequestBooking.aspx" class="navLink" target="_parent">New Booking</a></div>
        </td>
        </tr>
        <tr>
        <td align="center">
        <div class="tableBody">
        <table width="100%">
            <%
                //grab all bookings for this user
                Bookings bkList = new Bookings();
                bkList.Populate("wontExist", "txtUserName='" + userName + "' OR txtAssignedUser='" + userName + "'");
                List<Booking> bookings = bkList.getBookingList();

                if (bookings.Count() < 1)
                {
                    Response.Write("<tr>" + "You have no bookings in the system" + "</tr>");
                }
                else
                {
                    //verify that they have bookings that are active (some users may not have used the system in a while!)
                    Boolean statusCount = false ;
                    foreach (Booking book in bookings)
                    {
                        if (book.bookActive == true) { 
                            statusCount = true;
                            break;
                        }
                    }

                    if (!statusCount)
                    {
                        Response.Write("<tr>" + "You have no bookings in the system" + "</tr>");

                    }
                    else
                    {
                        int loopCount = 0;

                        //arrange bookings so latest start first
                        bookings.Sort(delegate(Booking a, Booking b)
                        {
                            int x = a.bookStart.CompareTo(b.bookStart);
                            if (x != 0) return -x;  //just make this positive to resource the order ;)
                            else return a.bookStart.CompareTo(b.bookStart);
                        });
                        
                        foreach (Booking book in bookings)
                        {
                            if (book.bookActive == true)
                            {
                                Response.Write("<tr>");
                                Response.Write("<td>");
                                Response.Write("<b>" + book.bookTitle + "</b>");
                                Response.Write("</td>");
                                Response.Write("<td>");
                                Response.Write(book.bookStart.ToString("ddd d MMM yyyy") + " " + book.bookStart.ToShortTimeString() + " - " + book.bookEnd.ToString("ddd d MMM yyyy") + " " + book.bookEnd.ToShortTimeString());
                                TimeSpan ts = book.bookEnd - book.bookStart;
                                if (ts.TotalDays < 1)
                                {
                                    Response.Write(" (" + Math.Round(ts.TotalHours, 1) + " hrs)");
                                }
                                else
                                {
                                    Response.Write(" (" + Math.Round(ts.TotalDays, 1) + " days)");
                                }
                                
                                //Does it recur? If so, output interval
                                if (book.bookRecurStatus == true)
                                {
                                    Response.Write(" [" + book.bookRecurInterval + "]");
                                }
                                
                                Response.Write("</td>");
                                Response.Write("<td>");
                                Response.Write("<b>Status: </b>");
                                Response.Write(book.bookStatus);
                                Response.Write(" (Ref: " + book.bookID + ")");
                                Response.Write("</td></tr>");
                                Response.Write("<tr>");
                                Response.Write("<td>");
                                if (book.bookClient == "" || book.bookClient == "Internal")
                                {
                                    Response.Write("Internal Booking");
                                }
                                else
                                {
                                    Response.Write("Expected: " + book.bookClient);
                                }
                                Response.Write("</td>");                                
                                Response.Write("<td>");
                                Response.Write("<i>");
                                //Look up resources for this booking so locations can be output
                                ResourceRequests resList = book.getResources();
                                List<ResourceRequest> resources = resList.getResourceRequests();                           
                                //Likewise for rooms
                                RoomRequests roomList = book.getRooms();
                                List<RoomRequest> rooms = roomList.getRoomRequests();
                                Response.Write("Requested ");
                                //output them
                                if (resources.Count() > 0)
                                {
                                    foreach (ResourceRequest res in resources)
                                    {
                                        Response.Write(res.ResName);
                                        if (resources.IndexOf(res) < resources.IndexOf(resources.Last()))
                                        {
                                            Response.Write(", ");
                                        }
                                    }
                                }

                                if (resources.Count() > 0 && rooms.Count() > 0)
                                {
                                    Response.Write(" @ ");
                                }
                                
                                if (rooms.Count() > 0)
                                {
                                    if (resources.Count() < 1)
                                    {
                                        Response.Write("access to ");
                                    }
                                    
                                    foreach (RoomRequest room in rooms)
                                    {
                                        Response.Write(room.RoomName);
                                        if (rooms.IndexOf(room) < rooms.IndexOf(rooms.Last()))
                                        {
                                            Response.Write(", ");
                                        }
                                    }
                                }
                                Response.Write("</i>");
                                Response.Write("</td>");
                                //Output member of staff responsible & any external clients expected
                                Response.Write("<td>");
                                UserRecord assignee = new UserRecord();
                                if (book.bookUserAssigned == "")
                                {
                                    assignee.Load(book.bookUserAssigned);
                                }
                                else
                                {
                                    assignee.Load(book.bookCreator);
                                }
                                Response.Write("<a href=\"mailto:" + assignee.userEmail + "?subject=Booking%20Query%20Ref%20" + book.bookID +"\">Contact " + assignee.userFirstName + " " + assignee.userSecondName + "</a>");
                                Response.Write("</td>");                                    
                                Response.Write("</tr>");

                                if (loopCount < bookings.Count() - 1)
                                {
                                    Response.Write("<tr><td colspan=\"3\"><hr /></td></tr>");
                                }
                                loopCount++;
                            }
                        }
                    }
                }    
                
            %>
        </table>        
        </div>
        </td>
        </tr>
        </table>
        
    </td>
    <td width="50%" valign="top">
        <table class="tableDefault" width="100%">
        <tr>
        <td align="center"><div class="tableTitle">
        <%=coloquialName %>'s Maintenance Tickets</div>
        <div class="navBar"><a href="RequestWorkTicket.aspx" class="navLink" target="_parent">New Request</a></div></td>
        </tr>
        <tr>
        <td align="center"><div class="tableBody">
        <table width="100%">
        <%
            WorkTickets wtList = new WorkTickets();
            wtList.Populate("wontExist", "txtUsername='" + userName + "' OR txtAllocatedTo='" + userName + "'");
            List<WorkTicket> tickets = wtList.getWorkTicketList();

            if (tickets.Count() < 1)
            {
                Response.Write("<tr>" + "You have no work tickets posted." + "</tr>");
            }
            else
            {
                //Check we actually have active tickets (a user may have past tickets)
                Boolean wtCheck = false;
                foreach (WorkTicket title in tickets)
                {
                    if (title.workTicketActive == true) {
                        wtCheck = true;
                        break;
                    }
                }
                if (!wtCheck)
                {
                    Response.Write("<tr>" + "You have no work tickets posted." + "</tr>");
                }
                else
                {
                    int loopCount = 0;

                    //arrange bookings so most recently created ticket appears first
                    tickets.Sort(delegate(WorkTicket a, WorkTicket b)
                    {
                        int x = a.createdTimeStamp.CompareTo(b.createdTimeStamp);
                        if (x != 0) return -x;  //just make this positive to resource the order ;)
                        else return a.createdTimeStamp.CompareTo(b.createdTimeStamp);
                    });
                    
                    
                    foreach (WorkTicket title in tickets)
                    {
                        if (title.workTicketActive == true)
                        {
                            Response.Write("<table width=\"100%\" class=\"workTicketDisplay\">");
                            Response.Write("<tr>");
                            Response.Write("<td>");
                            Response.Write("<b>" + title.workTicketTitle + "</b>");
                            Response.Write("</td>");
                            Response.Write("<td width=\"20%\">");
                            Response.Write("<i>" + title.workTicketLocation + "</i>");
                            Response.Write("</td>");
                            Response.Write("<td width=\"40%\">");
                            Response.Write("<b>Status: </b><a href=\"EditWorkTicket.aspx?wtID=" + title.workTicketID + "\" target=\"_parent\">");

                            //double-check not already complete
                            WorkTicketLogs wtLogs = new WorkTicketLogs();
                            wtLogs.Populate(title.workTicketID);
                            List<WorkTicketLog> wtLogList = wtLogs.getWorkTicketLogList();
                            if (title.workTicketApprovalStatus == true)
                            { 
                                Boolean completeCheck = false;
                                foreach (WorkTicketLog wtLog in wtLogList)
                                {
                                    if (wtLog.workTicketLogCategory == "Complete")
                                    {
                                        completeCheck = true;
                                        break;
                                    }
                                }

                                if (!completeCheck)
                                {
                                    UserRecord allocUser = new UserRecord();
                                    allocUser.Load(title.workTicketAllocatedTo);
                                    Response.Write(allocUser.userFirstName + " " + allocUser.userSecondName + " is working on this.");
                                }
                                else
                                {
                                    Response.Write("Marked Complete.");
                                }
                            }
                            else
                            {
                                Response.Write("Pending approval");
                            }
                            Response.Write("</a>");
                            Response.Write("</td></tr>");
                            Response.Write("<tr><td>");
                            Response.Write("Posted: " + title.createdTimeStamp.ToShortDateString() + " " + title.createdTimeStamp.ToShortTimeString());
                            Response.Write("</td>");
                            Response.Write("<td>");
                            
                            if (wtLogList.Count() > 0)
                            {
                                DateTime maxTime = new DateTime(1900, 01, 01, 00, 00, 00);

                                //arrange bookings so most recently created ticket appears first
                                wtLogList.Sort(delegate(WorkTicketLog a, WorkTicketLog b)
                                {
                                    int x = a.workTicketLogTime.CompareTo(b.workTicketLogTime);
                                    if (x != 0) return x;  //just make this positive to reverse the order ;)
                                    else return a.workTicketLogTime.CompareTo(b.workTicketLogTime);
                                });

                                WorkTicketLog mostRecent = wtLogList.Last();
                                Response.Write("Last update: " + mostRecent.workTicketLogTime.ToShortDateString() + " " + mostRecent.workTicketLogTime.ToShortTimeString());
                                
                            }
                            else
                            {
                                Response.Write("No updates yet.");
                            }
                            Response.Write("</td>");
                            Response.Write("<td>");
                            Response.Write("(Ref: " + title.workTicketID + ")");
                            Response.Write("</td>");
                            Response.Write("</tr>");

                            if (loopCount < tickets.Count()-1)
                            {
                                Response.Write("<tr><td colspan=\"3\"><hr /></td></tr>");
                            }
                            loopCount++;
                            
                            Response.Write("</table>");
                        }
                    }
                }
            }
            
         %>
        </table>
        </div></td>
        </tr>
            
        </table>
        <tr>
    <td colspan="2"><div class="footer">
    <% 
        Response.WriteFile(Server.MapPath("~/Include/footer.txt"));
    %>
    </div></td>
    </tr>
    </td>
    </tr>
    </table>
</body>
</html>
